import window from '@ohos.window';
import { WindowMode } from '@ohos.UiTest';

//外部调用窗口类
export class WindowManager {
  private static instace : WindowManager;
  private motorControlWindow : window.Window | null = null;
  private windowStage : window.WindowStage | null = null; //对应的页面stage配置
  private constructor() {} //构造器
  public static getInstace():WindowManager {
    //创建新的windowmanger
    if(!WindowManager.instace)
    {
      WindowManager.instace = new WindowManager();
    }
    return WindowManager.instace;
  }
  // 初始化窗口管理器
  public init(windowStage: window.WindowStage) {
    this.windowStage = windowStage;
  }
  public async showMotorControlWindow() : Promise<boolean> {
    try {
      if (this.motorControlWindow) { //未进行初始化电机悬浮窗口的话
        await this.motorControlWindow.showWindow() //直接显示
        return true
      }
      //假设未初始化窗口管理器
      if(!this.windowStage){
        console.error("can not init windowStage!")
        return false;
      }
      //创建窗口
      this.motorControlWindow = await this.windowStage.createSubWindow('MotorControlWindow'); //创建子窗口
      //设置窗口大小与背景框度
      // 设置窗口属性
      await this.setupMotorControlWindow();

      // 加载页面内容
      await this.motorControlWindow.setUIContent('pages/MotorControlPage');

      // 显示窗口
      await this.motorControlWindow.showWindow();
      this.motorControlWindow.setWindowBackgroundColor('#FFFFFF')
      console.info('MotorControl window created and shown successfully');
      return true;
    } catch (error) {
      console.error('Failed to create MotorControl window:', JSON.stringify(error));
      return false;
    }


  }
  private async  setupMotorControlWindow(){
    if(!this.motorControlWindow) {
      return;
    }
    try {

      // 设置窗口大小和位置
      const windowProperties = {
        x: 380,
        y: 80,
        width: 750,
        height: 1000
      };
      await this.motorControlWindow.moveWindowTo(windowProperties.x,windowProperties.y)
      await this.motorControlWindow.resize(windowProperties.width,windowProperties.height)

      //对应的调整大小操作
      // await this.motorControlWindow.setWindowResizable(true);
      await this.motorControlWindow.setWindowTouchable(true);
      await this.motorControlWindow.setWindowFocusable(true);
      // 设置窗口保持屏幕常亮（如果需要）
      await this.motorControlWindow.setWindowKeepScreenOn(true);
      //全屏显示数据
      // await this.motorControlWindow.setWindowSystemBarProperties()

    } catch (error) {
      console.error('Failed to create MotorControl window:', JSON.stringify(error));
    }
  }
  // 检查窗口是否存在且可见
  public isMotorControlWindowVisible(): boolean {
    return this.motorControlWindow !== null;
  }
  // 关闭并销毁MotorControl窗口
  public async closeMotorControlWindow(): Promise<boolean> {
    try {
      if (this.motorControlWindow) {
        await this.motorControlWindow.destroyWindow();
        this.motorControlWindow = null;
        console.info('MotorControl window destroyed successfully');
        return true;
      }
      return false;
    } catch (error) {
      console.error('Failed to destroy MotorControl window:', JSON.stringify(error));
      return false;
    }
  }
  //隐藏MotorControl窗口
  public async HideMotorControlWindow():Promise<boolean>{
    try {
      if(this.motorControlWindow){
        await this.motorControlWindow.destroyWindow()
        this.motorControlWindow = null
        console.info('Hide the MotorControl window Successful!')
      }

    } catch (error) {
      console.error('Failed to create MotorControl window:', JSON.stringify(error));
      return false;
    }
  }
}